/*
 * GPSWidget.cpp
 *
 *  Created on: Jan 21, 2011
 *      Author: kmueller
 */

#include "GPSWidget.h"

#include "PLLayout.h"

#include "../logic/Main.h"
#include "../logic/PositioningSystem.h"
#include "../logic/Settings.h"

#include <QtGui>

GPSWidget::GPSWidget(QWidget* parent) :
	QDialog(parent) {
	init();
	createLandscape();
	createPortrait();
	createAutoLayout();

	setLayout(landscapeLayout);
}

void GPSWidget::init() {
	setWindowTitle(tr("GPS-Information"));

	altitude = new QLabel();
	longitude = new QLabel();
	latitude = new QLabel();
	heading = new QLabel();
	velocity = new QLabel();
	time = new QLabel();
	fix = new QLabel();
	hAccuracy = new QLabel();
	vAccuracy = new QLabel();

	updateAll( NULL);

	Main &map = Main::getInstance();
connect(&map.getPositioningSystem(), SIGNAL(updated(const ExtendedGeoPositionInfo *)), this, SLOT(updateAll(const ExtendedGeoPositionInfo *)));
}

void GPSWidget::createLandscape() {
	QFormLayout *left = new QFormLayout();
	left->setLabelAlignment(Qt::AlignRight);
	left->setSpacing(5);

	QFormLayout *middle = new QFormLayout();
	middle->setLabelAlignment(Qt::AlignRight);
	middle->setSpacing(5);

	left->addRow(tr("Latitude:"), latitude);
	middle->addRow(tr("Longitude:"), longitude);
	left->addRow(tr("Altitude:"), altitude);
	middle->addRow(tr("Heading:"), heading);
	left->addRow(tr("Speed:"), velocity);
	middle->addRow(tr("Time:"), time);
	left->addRow(tr("Horizontal Accuracy:"), hAccuracy);
	middle->addRow(tr("Vertical Accuracy:"), vAccuracy);
	left->addRow(tr("Fix:"), fix);

	QPushButton *bClose = new QPushButton(tr("done"));
	connect(bClose, SIGNAL(clicked()), this, SLOT(close()));

	QBoxLayout *buttons = new QVBoxLayout();
	buttons->addStretch();
	buttons->addWidget(bClose);

	landscapeLayout = new QHBoxLayout();
	landscapeLayout->addLayout(left, 4);
	landscapeLayout->addLayout(middle, 4);
	landscapeLayout->addLayout(buttons, 1);
}

// TODO: muss noch an den Layout-Manager gebunden werden, damit das Drehen funktioniert und auch im Portrait-Modus das Portrait-Layout aktiviert wird.
void GPSWidget::createPortrait() {
	QFormLayout *left = new QFormLayout();
	QFormLayout *right = new QFormLayout();

	left->addRow(tr("Altitude:"), altitude);
	right->addRow(tr("Longitude:"), longitude);
	left->addRow(tr("Latitude:"), latitude);
	right->addRow(tr("Heading:"), heading);
	left->addRow(tr("Speed:"), velocity);
	right->addRow(tr("Time:"), time);
	left->addRow(tr("test"), time);

	QPushButton *bClose = new QPushButton(tr("done"));
	connect(bClose, SIGNAL(clicked()), this, SLOT(close()));

	QBoxLayout *contentLayout = new QVBoxLayout();
	contentLayout->addLayout(left);
	contentLayout->addLayout(right);

	portraitLayout = new QHBoxLayout();
	portraitLayout->addLayout(contentLayout);
	portraitLayout->addWidget(bClose);
}

void GPSWidget::createAutoLayout() {
	autoLayout = new PLLayout();
	autoLayout->setPLayout(portraitLayout);
	autoLayout->setLLayout(landscapeLayout);
}

void GPSWidget::updateAll(const ExtendedGeoPositionInfo* info) {
	setVelocity(info);
	setHeading(info);
	setCoordinates(info);
	setTime(info);
	setAccuracy(info);
	setFixType(info);
}

void GPSWidget::setVelocity(const ExtendedGeoPositionInfo* info) {
	AbstractSetting* s = Main::getInstance().getSettings().getUnit();
	velocity->setText(info != NULL ? QString::number(info->getVelocity(), 'f',
			1).append(" ").append(s->getDescription(s->getCurrent())) : tr(
			"n/a"));
}

void GPSWidget::setHeading(const ExtendedGeoPositionInfo* info) {
	heading->setText(info != NULL ? QString::number(info->getMovingDirection(),
			'f', 1).append(QString::fromUtf8("°")) : tr("n/a"));
}

void GPSWidget::setCoordinates(const ExtendedGeoPositionInfo* info) {

	if (info != NULL) {
		AbstractSetting* s =
				Main::getInstance().getSettings().getDegreeFormat();

		QGeoCoordinate::CoordinateFormat type = DegreeFormat::getQtFormatIdent(s->getCurrent());

		QString val = info->coordinate().toString(type);
		QStringList values = val.split(", ");

		switch (values.size()) {
		case 3:
			altitude->setText(values[2]);
		case 2:
			longitude->setText(values[1]);
		case 1:
			latitude->setText(values[0]);
		}

	} else {
		altitude->setText(tr("n/a"));
		longitude->setText(tr("n/a"));
		latitude->setText(tr("n/a"));
	}
}

void GPSWidget::setTime(const ExtendedGeoPositionInfo* info) {
	time->setText(info != NULL ? info->timestamp().toString("HH:mm:ss")
			: "00:00:00");
}

void GPSWidget::setFixType(const ExtendedGeoPositionInfo* info) {
	if (info != NULL) {
		switch (info->getFixType()) {
		case QGeoCoordinate::InvalidCoordinate:
			fix->setText(tr("invalid"));
			break;
		case QGeoCoordinate::Coordinate2D:
			fix->setText(tr("2D"));
			break;
		case QGeoCoordinate::Coordinate3D:
			fix->setText(tr("3D"));
			break;
		default:
			fix->setText(tr("n/a"));
		}
	} else {
		fix->setText(tr("n/a"));
	}
}

void GPSWidget::setAccuracy(const ExtendedGeoPositionInfo* info) {
	hAccuracy->setText(info != NULL ? QString::number(info->attribute(
			QGeoPositionInfo::HorizontalAccuracy), 'f', 0).append(" m") : tr(
			"n/a"));
	vAccuracy->setText(info != NULL ? QString::number(info->attribute(
			QGeoPositionInfo::VerticalAccuracy), 'f', 0).append(" m") : tr(
			"n/a"));

}

void GPSWidget::resizeEvent(QResizeEvent* event) {
	// TODO: turn handler darf nicht von event->size abhaengen.
	// Call the autoLayout convenience function for the event handling
	autoLayout->resizeEvent(event);
	// Call base class resize event handler
	QDialog::resizeEvent(event);
	//             #ifdef Q_OS_SYMBIAN
	//                 showMaximized();
	//                 #endif
}
